package com.java1234.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import com.java1234.dao.IUserDao;
import com.java1234.dao.impl.UserImpl;
import com.java1234.model.BookType;
import com.java1234.model.User;
import com.java1234.util.DbUtil;
import com.java1234.util.StringUtil;

public class LogOnFrm extends JFrame {

	private JPanel contentPane;
	private JTextField userAccountTxt;
	private JPasswordField passwordTxt;
	private JComboBox userTypeJcb;
	private DbUtil dbUtil = new DbUtil();
	private User user = new User();
	private IUserDao userDao = new UserImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LogOnFrm frame = new LogOnFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LogOnFrm() {
		// 改变系统默认字体
		Font font = new Font("Dialog", Font.PLAIN, 12);
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, font);
			}
		}
		setResizable(false);
		setTitle("登录系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 727, 568);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel lblNewLabel_1 = new JLabel("图书管理系统");
		lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 23));

		JLabel lblNewLabel_2 = new JLabel("用 户 名：");

		JLabel lblNewLabel_3 = new JLabel("密  码：");

		userAccountTxt = new JTextField();
		userAccountTxt.setColumns(10);

		passwordTxt = new JPasswordField();

		JButton btnNewButton = new JButton("登录");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginActionPerformed(e);
			}
		});

		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});

		JLabel lblNewLabel = new JLabel("用户类型：");

		userTypeJcb = this.getUserComboBox();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup().addGap(268).addComponent(lblNewLabel_1))
				.addGroup(gl_contentPane.createSequentialGroup().addGap(161).addComponent(btnNewButton).addGap(77)
						.addComponent(btnNewButton_1))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
						.addContainerGap(134, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addComponent(lblNewLabel)
								.addComponent(lblNewLabel_3).addComponent(lblNewLabel_2))
						.addGap(69)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(userTypeJcb, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(passwordTxt, Alignment.TRAILING).addComponent(userAccountTxt,
										Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))
						.addGap(259)));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane
				.createSequentialGroup().addGap(56).addComponent(lblNewLabel_1).addGap(138)
				.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel).addComponent(
						userTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(38)
				.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(userAccountTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2))
				.addGap(26)
				.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_3)
						.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE))
				.addGap(61).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
				.addContainerGap(84, Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);
		// 设置JFrame居中显示
		this.setLocationRelativeTo(null);
	}

	/**
	 * 登录事件处理
	 * 
	 * @param e
	 */
	protected void loginActionPerformed(ActionEvent e) {
		String userAccount = this.userAccountTxt.getText();
		String password = new String(this.passwordTxt.getPassword());
		if (StringUtil.isEmpty(userAccount)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空!");
			return;
		}
		if (StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空!");
			return;
		}
		User user = (User) userTypeJcb.getSelectedItem();
		String userType = user.getUserType();

		user = new User(userType, userAccount, password);

		Connection con = null;
		try {
			con = dbUtil.getCon();
			User currentUser = userDao.login(con, user);
			if (currentUser != null && userType.equals("系统管理员")) {
				dispose();
				new MainFrm().setVisible(true);
			} else if (currentUser != null && userType.equals("图书管理员")) {
				dispose();
				new LibrarianMainFrm().setVisible(true);
			} else if (currentUser != null && userType.equals("借阅者")) {
				dispose();
				new BorrowerMainFrm().setVisible(true);
			} else {
				JOptionPane.showMessageDialog(null, "用户名或密码错误！");
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 初始化用户类型下拉框
	 */

	private List<User> findUsers() {
		List<User> list = new ArrayList<User>();
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = userDao.list(con, new User());
			while (rs.next()) {
				user = new User();
				user.setUserType(rs.getString("userType"));
				user.setUserAccount(rs.getString("userAccount"));
				user.setPassword(rs.getString("password"));
				list.add(user);
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	private JComboBox getUserComboBox() {
		JComboBox jcb = new JComboBox();
		List<User> list = findUsers();
		for (User user : list) {
			jcb.addItem(user);
		}
		return jcb;
	}

	/**
	 * 重置事件处理
	 * 
	 * @param e
	 */
	private void resetValueActionPerformed(ActionEvent e) {
		if (this.userTypeJcb.getItemCount() > 0) {
			this.userTypeJcb.setSelectedIndex(0);
		}
		this.userAccountTxt.setText("");
		this.passwordTxt.setText("");

	}
}
